<?php
namespace Personnelsystem\Controller;
use Think\Controller;
use Think\Model;

class LibController{
    public $relist = null; //所有下属地区，用于sql查找
    public $father_Regione=null;

    //递归获取所选地区下的所有下属地区
    public function createtree(array $data , $pId) {
        $flag=0;
        foreach($data as $key => $val ){
            if($val['pId']==$pId)
            {
                $flag=1;
                //$this->relist[]=$val;    						//获取所选地区下的所有地区
                $botton=$this->createtree($data,$val['id']);	//只获所有地区下，最底层地区，减轻数据库运算
                if($botton){
                    $this->relist.=','.$val['id'];
                }
            }
        }
        if(empty($flag)){
            return ture;
        }else
            return false;
    }

    //递归获取所选地区上级地区
    public function getFatherRegione(array $data , $id) {

        foreach($data as $key => $val ){
            if($val['id']==$id&&!empty($val['pId']))
            {
                $this->father_Regione[]=$val;
                $this->getFatherRegione($data,$val['pId']);
            }
        }
    }

    //按年查询统计，分成12个月份
    public function  statisticsYear($date,$year,$userSum){
        $chartdate['datelist']=array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
        foreach($date as $key => $val){
            foreach($chartdate['datelist'] as $k => $v){
                $startime=$year.'-'.($k+1).'-01';
                $endtime=$year.'-'.($k+1).'-01 +1 month';
                if($val['ktime']>strtotime($startime)&&$val['ktime']<strtotime($endtime)){
                    $userdate[$k][$val['stId']]['number']++;
                    $userdate[$k][$val['stId']]['name']=$val['stName'];
                }
            }

        }

        //统计分段内考勤最大值和人均
        foreach($userdate as $key => $val){
            $maxkaoqin=0;  //出勤最多的人
            $sumkaoqin=0;
            foreach($val as $k => $v){
                $sumkaoqin+=$v['number'];
                if($v['number']>$maxkaoqin){
                    $maxkaoqin=$v['number'];
                }
            }
            $chartdate['userlist'][$key]['max']=$maxkaoqin;
            $chartdate['userlist'][$key]['average']=sprintf("%.2f", $sumkaoqin/$userSum);
        }

        foreach($chartdate['datelist'] as $key => $val){
            $chartdate['ratelist'][0][]=count($userdate[$key])/$userSum*100;
        }
        return $chartdate;
    }

    //按月查询统计，分成每一个星期
    public function statisticsMonth($date,$year,$month,$userSum){
        $week_arr=$this->getweekofmonth($year,$month);
        foreach($date as $key => $val){
            foreach($week_arr as $k => $v){
                if($val['ktime']>strtotime($v['start'])&&$val['ktime']<strtotime($v['end'])){
                    $userdate[$k][$val['stId']]['number']++;
                    $userdate[$k][$val['stId']]['name']=$val['stName'];
                }
            }
        }
        //统计分段内考勤最大值和人均
        foreach($userdate as $key => $val){
            $maxkaoqin=0;  //出勤最多的人
            $sumkaoqin=0;
            foreach($val as $k => $v){
                $sumkaoqin+=$v['number'];
                if($v['number']>$maxkaoqin){
                    $maxkaoqin=$v['number'];
                }
            }
            $chartdate['userlist'][$key]['max']=$maxkaoqin;
            $chartdate['userlist'][$key]['average']=sprintf("%.2f", $sumkaoqin/$userSum);
        }

        foreach($week_arr as $key => $val){
            $chartdate['datelist'][]=$val['start']."~".$val['end'];
            $chartdate['ratelist'][0][]=count($userdate[$key])/$userSum*100;
        }
        return $chartdate;
    }
    //按周查询统计，分成每一个天
    public function statisticsWeek($date,$week,$userSum){
        $chartdate['datelist']=array("星期一","星期二","星期三","星期四","星期五","星期六","星期日");
        $searchdate=explode('|',$week);
        foreach($date as $key => $val){
          foreach($chartdate['datelist'] as $k => $v){
              $startime=$searchdate[0]." +".$k." day";
              $endtime=$searchdate[0]." +".($k+1)." day";
              if($val['ktime']>strtotime($startime)&&$val['ktime']<strtotime($endtime)){
                  $userdate[$k][$val['stId']]['number']++;
                  $userdate[$k][$val['stId']]['name']=$val['stName'];
              }
          }
        }
        //统计分段内考勤最大值和人均
        foreach($userdate as $key => $val){
            $maxkaoqin=0;  //出勤最多的人
            $sumkaoqin=0;
            foreach($val as $k => $v){
                $sumkaoqin+=$v['number'];
                if($v['number']>$maxkaoqin){
                    $maxkaoqin=$v['number'];
                }
            }
            $chartdate['userlist'][$key]['max']=$maxkaoqin;
            $chartdate['userlist'][$key]['average']=sprintf("%.2f", $sumkaoqin/$userSum);
        }
        foreach($chartdate['datelist'] as $key => $val){
            $chartdate['ratelist'][0][]=count($userdate[$key])/$userSum*100;
        }
        return $chartdate;
    }

    /**
     * 查询每月的周数组
     */
    private function getweekofmonth($year,$month){
        //该月第一天
        $firstday = strtotime($year.'-'.$month.'-01');
        //该月的第一周有几天
        $firstweekday = (7 - date('N',$firstday) +1);
        //计算该月第一个周一的时间
        $starttime = $firstday-3600*24*(7-$firstweekday);
        //该月的最后一天
        $lastday = strtotime($year.'-'.$month.'-01'." +1 month -1 day");
        //该月的最后一周有几天
        $lastweekday = date('N',$lastday);
        //该月的最后一个周末的时间
        $endtime = $lastday-3600*24*$lastweekday;
        $step = 3600*24*7;//步长值
        $week_arr = array();
        for ($i=$starttime; $i<$endtime; $i= $i+3600*24*7){
            $week_arr[] = array('start'=>date('Y-m-d',$i), 'end'=>date('Y-m-d',$i+3600*24*7));
        }
        return $week_arr;
    }
}